import Vue from 'vue';
import CuLogin from './Login';

const defaultOptions = {
  title: '',
  width: '800',
};

let instance;

function init() {
  if (!instance || !document.body.contains(instance.$el)) {
    if (instance) {
      instance.$root.$destory();
    }
    instance = new (Vue.extend(CuLogin))({
      el: document.createElement('div'),
    });
    document.body.appendChild(instance.$el);
  }
  return instance;
}

function Login(options = {}) {
  let login = init();
  options = Object.assign(defaultOptions, options);
  Object.assign(login, options);
  return login;
}

Login.show = () => {
  return Login({ show: true });
};

export default {
  install: (Vue) => {
    Vue.prototype.$login = Login;
  },
};
